package com.wangbo.rabbit;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Slf4j
@Configuration
public class RabbitConfig {
    @Resource
    private MyMessageHandler myMessageHandler;

    /**
     * 定义一个队列监听容器
     */
    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerContainer(ConnectionFactory connectionFactory) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setConcurrentConsumers(2);
        container.setMaxConcurrentConsumers(10);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL); // RabbitMQ默认是自动确认，这里改为手动确认消息

        container.setQueueNames(Constant.DIRECT_QUEUE_NAME); // 设置监听队列

        container.setMessageListener(myMessageHandler); // 设置消息处理

        container.setAutoStartup(false); // 设置默认不启动

        container.setPrefetchCount(10); // 设置每次拉取并消费的消息条数，每次最多拉取并消费10条消息。

        return container;
    }
}
